import Database from 'better-sqlite3';
import fs from 'fs';
import path from 'path';
export default function handler(req, res) {
  // 连接到 Calibre 数据库
  const dbPath = path.normalize("C:\\Users\\Lame\\Calibre 书库\\metadata.db")
  const db = new Database(dbPath, { readonly: true });

  try {

    // {
    //     id: 19,
    //     title: '深入REACT技术栈',
    //     sort: '深入REACT技术栈',
    //     timestamp: '2023-10-23 18:57:03.078947+00:00',
    //     pubdate: '2017-02-17 09:32:05+00:00',
    //     series_index: 1,
    //     author_sort: '未知',
    //     isbn: '',
    //     lccn: '',
    //     path: 'Wei Zhi/Shen Ru REACTJi Zhu Zhan (19)',
    //     flags: 1,
    //     uuid: 'be8336fa-684d-4087-b361-26415b0a5613',
    //     has_cover: 1,
    //     last_modified: '2023-10-23 18:57:03.154180+00:00'
    //   }

    // 查询书籍信息
    const query = `
      SELECT *
      FROM books
      ORDER BY title ASC;
    `;
    const books = db.prepare(query).all();

    // console.log(books);

    // 返回 JSON 数据
    res.status(200).json({ books });
  } catch (error) {
    console.error('Error fetching books:', error);
    res.status(500).json({ error: 'Failed to fetch books' });
  } finally {
    db.close();
  }
}